package com.bjpowernode.web.controller;

import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.vo.ProductBidInfo;
import com.bjpowernode.contacts.YLBConst;
import com.bjpowernode.enums.Code;
import com.bjpowernode.web.views.ResultListView;
import com.bjpowernode.web.views.ResultView;
import com.bjpowernode.web.vo.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName:ProductController
 * Date:2021/12/13 17:17
 */

@CrossOrigin
@Api(tags = "理财产品功能信息")
@RestController
public class ProductController extends BaseController {


    /**
     * 查询单个产品
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "单个产品详情", notes = "根据产品的id，查询产品的完整数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品的主键Id", required = true)})
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功", response = ProductInfo.class),
            @ApiResponse(code = 500, message = "请求异常")})
    @GetMapping("/v1/product/detail")
    public ResultView queryByProductId(@RequestParam("id") Integer id) {

        ResultView<ProductBidInfo> resultView = ResultView.FAIL();
        if (id == null || id < 1) {
            resultView.setEnumCode(Code.PARAMETER_NULL);
        } else {
            //请求参数是正确的，查询数据
            ProductInfo productInfo = productService.queryProductByProductId(id);
            if (productInfo != null) {
                List<ProductBidInfo> productBidInfoList = investService.queryBidListByProductId(id, 1, 5);

                resultView = ResultView.SUCCESS();
                resultView.setObject(productInfo);
                resultView.setList(productBidInfoList);
            } else {
                resultView.setEnumCode(Code.PRODUCT_NOT_EXISTS);
            }
        }
        return resultView;
    }

    /**
     * 查询首页的多个产品
     *
     * @return
     */
    @ApiOperation(value = "项目首页多个产品列表", notes = "一个新手宝产品，三个优选类产品，三个散标类产品")
    @GetMapping("/v1/products/index")
    public ResultListView queryIndexProducts() {
        ResultListView resultListView = new ResultListView();
        resultListView.setCode(Code.SUCCESS.getCode());
        resultListView.setMsg(Code.SUCCESS.getText());

        List<ProductInfo> xinShouBAOList = productService.queryPageByType(YLBConst.PRODUCT_TYPE_XINSHOUBAO, 1, 1);
        List<ProductInfo> youXuanList = productService.queryPageByType(YLBConst.PRODUCT_TYPE_YOUXUAN, 1, 3);
        List<ProductInfo> sanBiaoList = productService.queryPageByType(YLBConst.PRODUCT_TYPE_SANBIAO, 1, 3);
        resultListView.setSanBiao(sanBiaoList);
        resultListView.setYouXuan(youXuanList);
        resultListView.setXinShouBao(xinShouBAOList);

        return resultListView;
    }

    /*分页产品 优选或者散标的产品*/
    @ApiOperation(value = "分页查询产品", notes = "使用产品类型作为参数，查询产品")
    @GetMapping("/v1/products/list")
    public ResultView queryCountOfProductByType(@RequestParam("productType") Integer productType,
                                                @RequestParam("pageNo") Integer pageNo) {
        ResultView<ProductInfo> resultView = ResultView.SUCCESS();
        //判断参数是有效的
        if (productType == null || productType < 0) {
            resultView.setEnumCode(Code.PARAMETER_NULL);
        } else {
            if (pageNo == null || pageNo < 1) {
                pageNo = 1;
            }
            //产品列表
            List<ProductInfo> productInfoList = productService.queryPageByType(productType, pageNo, Page.PAGE_SIZE);
            //page对象
            Integer totalRecord = productService.queryCountOfProductByType(productType);
            Page page = new Page(pageNo, Page.PAGE_SIZE, totalRecord);

            resultView.setList(productInfoList);
            resultView.setPage(page);
        }
        return resultView;
    }
}
